<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/6
 * Time: 14:40
 */
namespace App\Http\Controllers\Api\Notify;

use App\Http\Controllers\Controller;
use App\Repositories\CollectMoneyBillRepository;
use App\Repositories\PersonalAccountWithdrawRepository;
use App\Repositories\RepaymentAccountWithdrawRepository;
use App\Repositories\RepaymentPlanConsumeRepository;
use App\Repositories\RepaymentPlanFailHandlersRepository;
use App\Repositories\RepaymentPlanRepository;
use App\Repositories\ThreeApiErrorLogsRepository;
use App\Repositories\UserCreditCardRepository;
use App\Repositories\UserPassageRegisterRepository;
use App\Repositories\UserRepository;
use App\Services\CollectMoneyProfitService;
use App\Services\MessageService;
use App\Services\RepaymentProfitService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PaymentNotifyController extends Controller
{
    protected $threeApiErrorLogsRepository;
    protected $repaymentProfitService;
    protected $repaymentAccountWithdrawRepository;
    protected $personalAccountWithdrawRepository;
    protected $userRepository;
    protected $userCreditCardRepository;
    protected $repaymentPlanFailHandlersRepository;
    protected $messageService;
    protected $apiKey;

    public function __construct(ThreeApiErrorLogsRepository $threeApiErrorLogsRepository,
                                RepaymentProfitService $repaymentProfitService,
                                RepaymentAccountWithdrawRepository $repaymentAccountWithdrawRepository,
                                PersonalAccountWithdrawRepository $personalAccountWithdrawRepository,
                                UserRepository $userRepository,
                                UserCreditCardRepository $userCreditCardRepository,
                                RepaymentPlanFailHandlersRepository $repaymentPlanFailHandlersRepository,
                                MessageService $messageService)
    {
        $this->threeApiErrorLogsRepository = $threeApiErrorLogsRepository;
        $this->repaymentProfitService = $repaymentProfitService;
        $this->repaymentAccountWithdrawRepository = $repaymentAccountWithdrawRepository;
        $this->personalAccountWithdrawRepository = $personalAccountWithdrawRepository;
        $this->userRepository = $userRepository;
        $this->userCreditCardRepository = $userCreditCardRepository;
        $this->repaymentPlanFailHandlersRepository = $repaymentPlanFailHandlersRepository;
        $this->messageService = $messageService;
        $this->apiKey = config('pay.fourpay.api_key');
    }

    /**
     * 代付回调
     * {
     * "sign": "7aca35d53b14e4e4af07fa984cc09181",
     * "message": "支付成功",
     * "amount": "20.00",
     * "dsorderid": "A821456714614513",
     * "subMerBalance": "23.06",
     * "code": "SUCCESS"
     * }
     *  {"sign":"7aca35d53b14e4e4af07fa984cc09181","message":"\u652f\u4ed8\u6210\u529f","amount":"20.00","dsorderid":"A821456714614513","subMerBalance":"23.06","code":"SUCCESS"}
     */
    public function trade(Request $request)
    {
        Log::info('repaymentNotifyG:' . json_encode($request->all()));
        $param = $request->all();
        $sign = $param['sign'];
        unset($param['sign']);
        if ($sign != getSign($param, $this->apiKey)) {
            Log::info('代付G交易回调签名验证不正确');
            die('代付G交易回调签名验证不正确');
        }
        $field = $this->personalAccountWithdrawRepository->byWithdrawNo($param['mch_no']);
        $user = $this->userRepository->byId($field->user_id);
        if ($param['order_status'] == 'SUCCESS') {
            $this->personalAccountWithdrawRepository->updateStatusByWithdrawNo($param['mch_no'], 5002);//成功
            //发送账户余额提现成功消息提醒
            $this->messageService->messageTemplate(13, $user->invited_code, $field->bank_no, $field->bank_description,
                $field->withdraw_money, '', '', $field->id, '', '');
        } else {
            $this->personalAccountWithdrawRepository->updateStatusByWithdrawNo($param['mch_no'], 5003);//失败
            //账户余额加
            if (!empty($field)) {
                $userProfit = $this->userRepository->byId($field->user_id);
                $accountBalance = $userProfit->account_balance + $field->withdraw_money;
                $userProfit->update(['account_balance' => $accountBalance]);
            }
            //发送账户余额提现失败消息提醒
            $this->messageService->messageTemplate(14, $user->invited_code, $field->bank_no, $field->bank_description,
                $field->withdraw_money, '', '', $field->id, '', '');
        }
        echo 'SUCCESS';
    }
}